package com.duvee.getInfo;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.duvee.uiautomator.bean.PhoneInfoBean;

/**
 * 根据xml文件，获取对应信息
 * 
 * @author duvee
 * 
 */
public class BaseGetXml {
	String xmlString, productName;

	public BaseGetXml(String xmlString, String productName) {
		this.xmlString = xmlString;
		this.productName = productName;
	}

	/**
	 * 获取手机机型信息
	 * 
	 * @return
	 */
	public PhoneInfoBean getPhoneInfo() {
		PhoneInfoBean phoneBean = new PhoneInfoBean();
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(xmlString);
			NodeList dogList = doc.getElementsByTagName(productName);
			Node dog = dogList.item(0);
			for (Node node = dog.getFirstChild(); node != null; node = node
					.getNextSibling()) {
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					String name = node.getNodeName();
					if (!(node.getFirstChild().getNodeValue() == null)) {
						String value = node.getFirstChild().getNodeValue();
						if (name.equals("phoneProductName")) {
							phoneBean.setPhoneProductName(value);
						}
						if (name.equals("phoneTakePhotoID")) {
							phoneBean.setPhoneTakePhotoID(value);
						}
						if (name.equals("phoneSavePhotoID")) {
							phoneBean.setPhoneSavePhotoID(value);
						}
						if (name.equals("phoneCallID")) {
							phoneBean.setPhoneCallID(value);
						}
						if (name.equals("phoneEndCallID")) {
							phoneBean.setPhoneEndCallID(value);
						}
						if (name.equals("phoneSendMessageID")) {
							phoneBean.setPhoneSendMessageID(value);
						}
					}
				}
			}
			phoneBean.setPhoneProductName(productName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return phoneBean;
	}
}
